package com.karson.front.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.log.Log;
import com.karson.common.request.LoginMobileRequest;
import com.karson.common.request.LoginRequest;
import com.karson.common.response.CommonResult;
import com.karson.common.response.LoginAdminResponse;
import com.karson.common.response.LoginResponse;

import com.karson.common.vo.UserVo;
import com.karson.front.service.LoginService;
import com.karson.front.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录
 * @author ZKS
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "登录控制器接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SmsService smsService;

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public CommonResult<LoginResponse> login(@RequestBody @Validated LoginRequest loginRequest) {
        return CommonResult.success(loginService.login(loginRequest));
    }
    /**
     * 手机号登录接口
     */
    @Operation(summary = "手机号登录接口")
    @RequestMapping(value = "/login/mobile", method = RequestMethod.POST)
    public CommonResult<LoginResponse> phoneLogin(@RequestBody @Validated LoginMobileRequest loginRequest, HttpServletRequest request) {
        return CommonResult.success(loginService.phoneLogin(loginRequest,request));
    }
    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public CommonResult<Boolean> register(@RequestBody @Validated LoginRequest loginRequest, HttpServletRequest request) {
        return CommonResult.success(loginService.register(loginRequest,request));
    }
    @Operation(summary = "用户信息")
    @GetMapping("/info")
    public CommonResult<UserVo> info() {
         return CommonResult.success(loginService.getUserInfo());
    }
    /**
     * 发送短信登录验证码
     * @param phone 手机号码
     * @return 发送是否成功
     */
    @Operation(summary = "发送短信登录验证码")
    @PostMapping(value = "/sendCode")
    @Parameter(name = "phone", description = "手机号码", required = true)
    public CommonResult<Object> sendCode(@RequestParam String phone){
        if(smsService.sendCommonCode(phone)){
            return CommonResult.success("发送成功");
        }else{
            return CommonResult.failed("发送失败");
        }
    }

}
